package com.zang.blogz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.entity.Category;
import com.zang.blogz.model.output.admin.AdminCategoryVO;
import com.zang.blogz.model.output.TagArticleVO;
import com.zang.blogz.model.output.CategoryVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/blogz/category")
@Api(tags = "分类模块")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类页显示
     *
     * @return
     */
    @ApiOperation(value = "分类页所有信息", notes = "分类页所有信息")
    @GetMapping("/")
    public Result<CategoryVO> getCategoryList() {

        return Result.ok(categoryService.getCategoryList());
    }

    @ApiOperation(value = "根据分类id查询分类下所有文章", notes = "根据分类id查询分类下所有文章")
    @GetMapping("/queryListArticleByCategoryId")
    public Result<List<TagArticleVO>> getCategoryArticleVoById(@RequestParam(value = "categoryId") Integer categoryId) {

        return Result.ok(categoryService.getCategoryArticleVoById(categoryId));
    }

    @ApiOperation(value = "管理员分类页分页列表", notes = "管理员分类页分页列表")
    @GetMapping("/admin/page")
    public Result<IPage<AdminCategoryVO>> getCategoryPage(@RequestParam(value = "categoryName", required = false) String categoryName,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<Category> page = new Page<>(pageNo, pageSize);

        return Result.ok(categoryService.getCategoryPage(categoryName, page));
    }

    @ApiOperation(value = "编辑or新增分类", notes = "编辑or新增分类")
    @PostMapping("/admin/edit")
    @OptLog(optType = OptTypeConstant.SAVE_OR_UPDATE)
    public Result<Category> editOrSaveCategory(@RequestBody Category category) {

        return categoryService.editOrSaveCategory(category);
    }

    @ApiOperation(value = "根据分类id删除分类", notes = "根据分类id删除分类")
    @DeleteMapping("/admin/categoryId")
    @OptLog(optType = OptTypeConstant.REMOVE)
    public Result<String> deleteCategoryById(@RequestParam("categoryId") Integer categoryId) {

        return categoryService.deleteCategoryById(categoryId);
    }


}
